import 'dart:developer';

import 'package:amway_superapp_creator_plugin/api.dart';
import 'package:amway_superapp_creator_plugin/models/tag_item_bean.dart';
import 'package:amway_superapp_creator_plugin/utils/toast_util.dart';
import 'package:amway_superapp_creator_plugin/view_state.dart';
import 'package:amway_superapp_creator_plugin/viewmodels/base_model.dart';
import 'package:flutter/cupertino.dart';

class TagListVM extends BaseModel {
  final List<TagItemBean> hobbyList = new List();
  final List<TagItemBean> collectionList = new List();

  final Map<String, List<TagItemBean>> identityList = new Map();

  final List<TagItemBean> cachedList= new List();

  ///0代表身份标签，1代表兴趣标签
  int _pageType = 0;

  int get pageType => _pageType;


  TagListVM(int pageType, {@required Api api}) {
    this.api = api;
    this._pageType = pageType;
  }

  void add(TagItemBean item) {
    if (!collectionList.contains(item)) {
      collectionList.add(item);
      log("added");
      notifyListeners();
    }
  }

  void addAllHobbies(List<String> list) {
    for (int i = 0; i < list.length; i++) {
      hobbyList.add(new TagItemBean(list.elementAt(i), "hobbies"));
    }
    notifyListeners();
  }

  void addAllIdentities(Map<String, List<dynamic>> dataMap) {
    dataMap.forEach((key, value) {
      List<TagItemBean> list = new List();
      value.forEach((element) {
        list.add(new TagItemBean(element, "identities"));
      });
      identityList.putIfAbsent(key, () => list);
    });
    notifyListeners();
  }

  /// Removes all items from the cart.
  void remove(TagItemBean item) {
    collectionList.remove(item);
    notifyListeners();
  }

  void setData(List<String> cachedMarks) {
    setState(ViewState.Busy);
    if (_pageType == 1) {
      api.getHobbyList((data) {
        setState(ViewState.Idle);
        try {
          List<dynamic> list = data;
          List<String> stringList = List.from(list);
          addAllHobbies(stringList);
          processCaches(cachedMarks, "hobby");
        } on Exception catch (e) {
          // 任意一个异常
          print('Unknown exception: $e');
        } catch (e) {
          // 非具体类型
          print('Something really unknown: $e');
        }
      }, (data) {
        setState(ViewState.Idle);
        log(data.toString());
        ToastUtils.showShortToast('请求接口错误');
      });
    } else if (_pageType == 0) {
      api.getIdentityList((data) {
        setState(ViewState.Idle);
        try {
          Map<String, List<dynamic>> map = Map.from(data);
          addAllIdentities(map);
          processCaches(cachedMarks, 'identity');
        } on Exception catch (e) {
          // 任意一个异常
          print('Unknown exception: $e');
        } catch (e) {
          // 非具体类型
          print('Something really unknown: $e');
        }
      }, (data) {
        setState(ViewState.Idle);
        log(data.toString());
      });
    }
  }

  void processCaches(List<String> cachedMarks, String type) {
    if (type == 'identity') {
      identityList.forEach((key, list) {
        list.forEach((bean) {
          cachedMarks.forEach((s) {
            if (bean.title == s) {
              bean.isSelected = true;
              collectionList.add(bean);
              cachedList.add(bean);
            }
          });
        });
      });
    } else {
      hobbyList.forEach((bean) {
        cachedMarks.forEach((s) {
          if (bean.title == s) {
            bean.isSelected = true;
            collectionList.add(bean);
            cachedList.add(bean);
          }
        });
      });
    }
  }

  bool identicalToCache(){
    if(cachedList.length==collectionList.length){
      cachedList.sort((a,b)=>a.title.compareTo(b.title));
      collectionList.sort((a,b)=>a.title.compareTo(b.title));
      for(int i=0;i<cachedList.length;i++){
        if(cachedList[i].title!=collectionList[i].title){
          return false;
        }
      }
    }else{
      return false;
    }
    return true;
  }
}
